FROM nvidia/cudagl:11.4.0-devel-ubuntu20.04

ENV DEBIAN_FRONTEND=noninteractive

# Install build dependencies
RUN apt-get clean \
    && apt-get update \
    && apt-get -y install --no-install-recommends \
    build-essential \
    cmake \
    git \
    pkg-config \
    software-properties-common \
    && rm -rf /var/lib/apt/lists/*

# Install DART dependencies
RUN apt-get update \
    && apt-get -y install --no-install-recommends \
    libeigen3-dev \
    libassimp-dev \
    libccd-dev \
    libfcl-dev \
    libboost-all-dev \
    libnlopt-cxx-dev \
    coinor-libipopt-dev \
    libbullet-dev \
    liblz4-dev \
    libode-dev \
    liboctomap-dev \
    libflann-dev \
    libtinyxml2-dev \
    liburdfdom-dev \
    libxi-dev \
    libxmu-dev \
    freeglut3-dev \
    libopenscenegraph-dev \
    && rm -rf /var/lib/apt/lists/*

# Install dartpy dependencies
RUN apt-get update \
    && apt-get -y install --no-install-recommends \
    libpython3-dev \
    pybind11-dev \
    python3 \
    python3-pip \
    python3-pytest \
    python3-distutils \
    && rm -rf /var/lib/apt/lists/*

ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib:/usr/local/lib:/usr/lib/x86_64-linux-gnu

#######################################
# Compile and install DART and dartpy #
#######################################
WORKDIR /opt
RUN git clone https://github.com/dartsim/dart.git
WORKDIR /opt/dart
# Apply local patch
COPY docker/dart.patch .
RUN cd /opt/dart \
    && git checkout tags/v6.7.2 \
    && git apply dart.patch \
    && mkdir build \
    && cd build \
    && cmake .. \
    # && cmake \
    # -DCMAKE_INSTALL_PREFIX=/usr/ \
    # -DCMAKE_BUILD_TYPE=Release .. \
    # -DBUILD_SHARED_LIBS=ON \
    # -DDART_BUILD_DARTPY=ON \
    && make \
    && make install
    # && make dartpy \
    # && make install-dartpy

RUN ln -s /usr/bin/python3 /usr/bin/python


###############################
# Compile and install PyDart2 #
###############################
RUN python -m pip install numpy
RUN apt-get update && apt-get install -y --no-install-recommends swig
WORKDIR /opt
RUN git clone https://github.com/sehoonha/pydart2.git
WORKDIR /opt/pydart2
COPY docker/pydart2.patch .
RUN git checkout 113132a \
    && git apply pydart2.patch \
    && python setup.py build build_ext \
    && python setup.py develop

# Forcibly fix the problem in auto-generated pydart2_api.py
RUN sed -i 's/from . import _pydart2_api/import _pydart2_api/g' /opt/pydart2/pydart2/pydart2_api.py

###############################
# Compile and install DartEnv #
###############################
WORKDIR /opt
RUN git clone --branch nodisplay https://github.com/gtrll/dartenv.git
WORKDIR /opt/dartenv
COPY docker/dartenv.patch .
RUN cd /opt/dartenv \
    && git apply dartenv.patch \
    && pip install -e .[dart]
# DartEnv runtime dependency
RUN pip install PyQt5 joblib

# Project specific dependencies
RUN pip install GitPython matplotlib tensorflow

RUN pip install torch

# Install wandb and its (optional) dependencies
RUN pip install \
    wandb \
    kornia \
    ffmpeg \
    imageio \
    imageio-ffmpeg \
    moviepy

RUN pip install colorlog

# Virtual Display
# ENV LD_LIBRARY_PATH /root/.mujoco/mujoco210/bin:${LD_LIBRARY_PATH}
ENV LD_LIBRARY_PATH /usr/local/nvidia/lib64:${LD_LIBRARY_PATH}
COPY docker/vendor/Xdummy /usr/local/bin/Xdummy
COPY docker/vendor/Xdummy-entrypoint /usr/local/bin/Xdummy-entrypoint
RUN chmod +x /usr/local/bin/Xdummy /usr/local/bin/Xdummy-entrypoint

# install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    apt-utils \
    libffi-dev \
    libfreetype6-dev \
    libgl1-mesa-dev \
    libgl1-mesa-glx \
    libosmesa6-dev \
    libglfw3 \
    gcc \
    pciutils \
    xserver-xorg \
    xserver-xorg-video-fbdev \
    xauth

RUN apt-get install -y libgl1-mesa-glx libosmesa6 libglew-dev libglfw3 libglfw3-dev

RUN apt-get update -q \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    libosmesa6-dev \
    software-properties-common \
    net-tools \
    xpra \
    xserver-xorg-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Workaround for https://bugs.launchpad.net/ubuntu/+source/nvidia-graphics-drivers-375/+bug/1674677
COPY docker/vendor/10_nvidia.json /usr/share/glvnd/egl_vendor.d/10_nvidia.json

RUN pip install pfrl
RUN apt-get update && apt-get install -y --no-install-recommends \
    mesa-utils \
    libxmu-dev \
    libxi-dev \
    libglu1-mesa-dev \
    freeglut3-dev \
    mesa-common-dev

WORKDIR "/"

RUN pip install params_proto pandas

ENTRYPOINT ["/usr/local/bin/Xdummy-entrypoint"]
